package cn.pzhu.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.jasper.tagplugins.jstl.core.If;

import cn.pzhu.pojo.User;

/**
 * Application Lifecycle Listener implementation class OnlineListener
 *
 */
@WebListener
public class OnlineListener implements HttpSessionAttributeListener {

	private static int online =0;
    /**
     * Default constructor. 
     */
    public OnlineListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent se)  { 
         // TODO Auto-generated method stub
    	synchronized (OnlineListener.class) {
			if(se.getName().equals("user")){
				online++;
				se.getSession().getServletContext().setAttribute("online", online);
				if(se.getValue() instanceof User) {
					User user = (User)se.getValue();
					System.out.println(user.getUsername()+"登录");
				}
			}
		}
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent se)  { 
         // TODO Auto-generated method stub
    	synchronized (OnlineListener.class) {
			if(se.getName().equals("user")){
				online--;
				se.getSession().getServletContext().setAttribute("online", online);
				if(se.getValue() instanceof User) {
					User user = (User)se.getValue();
					System.out.println(user.getUsername()+"离线");
				}
			}
		}
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent se)  { 
         // TODO Auto-generated method stub
    }
	
}
